function getPatientSearchResult() {
	$("#appointPatientSearchResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#updateDiv").empty();
	$("#updateDiv").hide();
	$.getJSON("staff/appointPatientSearch.html?appointPatientId="+$("[name='appointPatientId']").val()+"&appointPatientFirstname="+$("[name='appointPatientFirstname']").val()+"&appointPatientLastname="+$("[name='appointPatientLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.appointPatientSearchResult)){
			$("#appointPatientSearchResult").data("searchResult", data);
			printTableBody(data);
			$("#appointPatientSearchResult").append("<br /><button id=\"confirmPatient\" onclick=\"confirmPatient()\" class=\"button\">confirm</button>&nbsp;");
		}else{
			$("#appointPatientSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function clearPtable(){
	$("#appointPatientSearchResult").empty();
}

function printTableBody(data) {
	
	$("#appointPatientSearchResult")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th></th>"+
					"<th>Patient id</th>" +
					"<th>First name</th>" +
					"<th>Last name</th>" +
					"<th>Gender</th>" +
					"<th>Date of Birth</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.appointPatientSearchResult).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td><input type=\"radio\" name=\"patientRadio\" value=\""+v.userId+"\"></td>");
					tr.append("<td>" + v.userId + "</td>");
					tr.append("<td>" + v.firstname + "</td>");
					tr.append("<td>" + v.lastname + "</td>");
					var gen=v.gender;
					if(gen=='m'){
						tr.append("<td>male</td>");
					}else if(gen=='f'){
						tr.append("<td>female</td>");
					}else{
						tr.append("<td></td>");
					}
					tr.append("<td>" + v.dateOfBirth.substring(0,10) +"</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ],
		headers : {
			0 : {
				sorter : false
			}
		}
	});
}

function confirmPatient(){
	var id=$("[name=\"patientRadio\"]:checked").val();
	$("#appointPatientSearchResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#updateDiv").empty();
	$("#updateDiv").hide();
	var patientInfo=null;
	var patientInfoList=$("#appointPatientSearchResult").data("searchResult");
	$(patientInfoList).each(function(index, value){
		$(value.appointPatientSearchResult).each(function (i,v){
			if(v.userId==id){
				patientInfo=v;
			}
		});
	});
	var dateOfBirth=new Date(patientInfo.dateOfBirth);
	$("#updateDiv").append("<form action=\"javascript:updatePatientInfo("+patientInfo.userId+")\" class=\"selectForm\">" +
			"<h1>update patient information</h1>" +
			"<label><span>First name:</span><input id=\"firstname\" type=\"text\" name=\"firstname\"/></label>" +
			"<label><span>Last name:</span><input id=\"lastname\" type=\"text\" name=\"lastname\"/></label>" +
			"<label><span>Date of birth:</span><input id=\"dateOfBirth\" name=\"date\" type=\"text\"></label>" +
			"<label><span>Gender:</span><select name=\"gender\"><option value=\"-1\" selected=\"selected\">Select gender</option><option value=\"male\">male</option><option value=\"female\">female</option></select></label>" +
			"<label><span>Home phone number:</span><input id=\"phoneNum\" type=\"text\" name=\"phoneNum\"/></label>" +
			"<label><span>Cellphone number:</span><input id=\"cellphone\" type=\"text\" name=\"cellphone\"/></label>" +
			"<label><span>Email:</span><input id=\"email\" type=\"text\" name=\"email\"/></label>" +
			"<label><span>Address:</span><textarea id=\"address\" name=\"address\" rows=\"3\" cols=\"30\"></textarea></label>" +
			"<label><span>Postal code:</span><input id=\"postalCode\" type=\"text\" name=\"postalCode\"/></label>" +
			"<label><span>Current health:</span><textarea id=\"currentHealth\" name=\"currentHealth\" rows=\"3\" cols=\"30\"></textarea></label>" +
			"<button id=\"updatePatientInfo\" type=\"submit\" class=\"button\">confirm</button>");
	$("[name='firstname']").val(patientInfo.firstname);
	$("[name='lastname']").val(patientInfo.lastname);
	/*
	$("[name='year']").val(dateOfBirth.getFullYear());
	$("[name='month']").val(dateOfBirth.getMonth()+1);
	$("[name='day']").val(dateOfBirth.getDate());
	*/
	$("#dateOfBirth").datepicker("setDate",dateOfBirth);
	$("#dateOfBirth").datepicker("update");
	if(patientInfo.gender=='m'){
		$("[name='gender']").val("male");
	}else{
		$("[name='gender']").val("female");
	}
	$("[name='phoneNum']").val(patientInfo.phonenum);
	$("[name='cellphone']").val(patientInfo.cellphone);
	$("[name='email']").val(patientInfo.email);
	$("[name='address']").val(patientInfo.address);
	$("[name='postalCode']").val(patientInfo.postalCode);
	$("[name='currentHealth']").val(patientInfo.currentHealth);
	$("#updateDiv").show();
	/*
	if(!$("#updatePatientInfo").length){
		$("#updateDiv").append("<button id=\"updatePatientInfo\" onclick=\"updatePatientInfo("+patientInfo.userId+")\" class=\"button\">confirm</button>");
	}
	*/
}

function updatePatientInfo(patientId){
	$.getJSON("staff/updatePatientInfo.html?updatePatientId="+patientId+"&firstname="+$("[name='firstname']").val()+"&lastname="+$("[name='lastname']").val()+"&date="+$("#dateOfBirth").val()
			+"&gender="+$("[name='gender']").val()+"&phoneNum="+$("[name='phoneNum']").val()+"&cellphone="+$("[name='cellphone']").val()+"&email="+$("[name='email']").val()
			+"&address="+$("[name='address']").val()+"&postalCode="+$("[name='postalCode']").val()+"&currentHealth="+$("[name='currentHealth']").val(),function(data){
		$("[name='appointPatientId']").val("");
		$("[name='appointPatientFirstname']").val("");
		$("[name='appointPatientLastname']").val("");
		if(data.flag==true){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("success");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
			$("#updateDiv").empty();
			$("#updateDiv").hide();
		}else if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}
	});
	
}

$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#updateDiv").empty();
	$("#updateDiv").hide();
	$("[name='appointPatientId']").val("");
	$("[name='appointPatientFirstname']").val("");
	$("[name='appointPatientLastname']").val("");
});